package javabase.schoolwork.day6_12;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author fance
 * @date 18-6-11 下午3:30
 */
public class MapSum {
    public void fun1() {
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,1);
        map.put(2,2);
        System.out.println(map.containsKey(1));
        System.out.println(map.containsKey(3));
        System.out.println(map.get(2));
        System.out.println(map.containsValue(3));

        Set<Integer> set = map.keySet();
        Collection<Integer> collection = map.values();
        for (Integer k : set) {
            System.out.print(k + " ");
        }
        System.out.println();
        for (Integer v : collection) {
            System.out.print(v + " ");
        }
        System.out.println();

        for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        Iterator<Map.Entry<Integer,Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer,Integer> cur = iterator.next();
            System.out.println(cur.getKey() + ":" + cur.getValue());
        }
    }
    public void fun2() {
        Map<Person,Integer> map = new TreeMap<>();
        for (int i = 0; i < 10; i++) {
            map.put(new Person(String.valueOf(i),i),i);
        }

        for (Map.Entry<Person,Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
    public void fun3() throws IOException {
        Properties properties = new Properties();
        properties.load(new InputStreamReader(MapSum.class.getClassLoader().getResourceAsStream("javabase/schoolwork/day6_12/test.properties"),"UTF-8"));
        System.out.println(properties.getProperty("fance"));
        System.out.println(properties.getProperty("age"));
    }

    public void fun4() throws InterruptedException {
        Map<String,String> map = new WeakHashMap<>();
        map.put(new String("1"),new String("10"));
        map.put(new String("2"),new String("20"));
        map.put(new String("3"),new String("30"));
        System.gc();

        Thread.sleep(1000);

        map.put(new String("4"),new String("40"));
        System.out.println(map);
    }
    public void fun5() throws InterruptedException {
        SortedMap<String,String> map = new TreeMap<>();
        map.put(new String("1"),new String("10"));
        map.put(new String("2"),new String("20"));
        map.put(new String("3"),new String("30"));
        map.put(new String("4"),new String("40"));

        System.out.println(map.firstKey());
        System.out.println(map.get(map.firstKey()));
        System.out.println(map.lastKey());
        System.out.println(map.get(map.lastKey()));
        System.out.println(map);
    }
    public void fun6() throws InterruptedException {

    }
    @Test
    public void test() throws IOException, InterruptedException {
//         fun1();
//        fun2();
//        fun3();
//        fun4();
        fun5();
    }
}
